package com.diver.flinkdemo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diver.flinkdemo.entity.Trd.zgyzh.UniversalReply;
import com.diver.flinkdemo.mapper.mysqlMapper.UniversalReplyMapper;
import com.diver.flinkdemo.service.IUniversalReplyService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

/**
 * @Author houmx
 * @Date 2022/11/8 17:06
 * @Describe
 */

//@Service
@Slf4j
public class UniversalReplyServiceImpl implements IUniversalReplyService {

    private UniversalReplyMapper mapper;
    private SqlSession session;

    @Override
    public UniversalReply getUniversalReply(String cardNum, int commandType, String distributionChannel) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(UniversalReplyMapper.class);
            QueryWrapper<UniversalReply> universalReplyQueryWrapper = new QueryWrapper<>();
            universalReplyQueryWrapper.eq("card_num", cardNum);
            universalReplyQueryWrapper.eq("type", commandType);
            universalReplyQueryWrapper.eq("distribution_channel", distributionChannel);
            universalReplyQueryWrapper.eq("is_result", 0);
            universalReplyQueryWrapper.orderByDesc("create_time");
            universalReplyQueryWrapper.last("limit 1");
            return mapper.selectOne(universalReplyQueryWrapper);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public Boolean updateUniversalReply(String id) {

        try {
            QueryWrapper<UniversalReply> universalReplyQueryWrapper = new QueryWrapper<>();
            universalReplyQueryWrapper.eq("id", id);
            UniversalReply universalReply = new UniversalReply();
            universalReply.setIsResult(1);
            mapper.update(universalReply, universalReplyQueryWrapper);
            return true;
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}


